package me.xiaoyan.ehr.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import me.xiaoyan.ehr.config.annotation.OperationLog;
import me.xiaoyan.ehr.po.SysNotice;
import me.xiaoyan.ehr.service.SysNoticeService;
import me.xiaoyan.ehr.vo.ApiResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/notice")
@ApiOperation(
        value = "通知管理"
)
public class SysNoticeController {
    private SysNoticeService sysNoticeService;

    @Autowired
    public void setSysNoticeService(SysNoticeService sysNoticeService) {
        this.sysNoticeService = sysNoticeService;
    }

    @GetMapping("/list")
    @ApiOperation(
            value = "查询通知列表", httpMethod = "GET", notes = "查询通知列表"
    )
    @OperationLog("通知管理")
    public ApiResponseData<IPage<SysNotice>> getList(@RequestParam Map<String, String> params) {
        IPage<SysNotice> list = sysNoticeService.selectByParams(params);
        ApiResponseData<IPage<SysNotice>> SysNoticeList = new ApiResponseData<>();
        SysNoticeList.setData(list);
        return SysNoticeList;
    }

    @GetMapping("/info")
    @ApiOperation(
            value = "查询通知信息", httpMethod = "GET", notes = "查询通知信息"
    )
    @OperationLog("通知管理")
    public ApiResponseData<IPage<SysNotice>> getInfo(@RequestParam Map<String, String> params) {
        IPage<SysNotice> list = sysNoticeService.selectByParams(params);
        ApiResponseData<IPage<SysNotice>> SysNoticeList = new ApiResponseData<>();
        SysNoticeList.setData(list);
        return SysNoticeList;
    }

    public ApiResponseData<SysNotice> getInfo(Integer id) {
        ApiResponseData<SysNotice> data = new ApiResponseData<>();
        SysNotice notice = sysNoticeService.getById(id);
        data.setData(notice);
        return data;
    }

    // 保存职位 当id为空时 则新增否则为保存
    @PostMapping("/save")
    @ApiOperation(
            value = "新增通知", httpMethod = "POST", notes = "新增通知"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "notice", value = "通知json对象", dataType = "SysNotice")
    })
    public ApiResponseData save(@RequestBody SysNotice notice) {
        ApiResponseData data = new ApiResponseData();
        if (notice.getId() != null && notice.getId() > 0) {
            sysNoticeService.updateById(notice); // 更新
        } else {
            sysNoticeService.save(notice); // 新增
        }
        return data;
    }

}
